﻿@model SQLInstance
@{
    var connections = Model.ConnectionsSummary.SafeData(true);
    var groups = connections.GroupBy(c => new { c.LoginName, c.TransactionIsolationLevel }).OrderByDescending(c => c.Sum(g => g.ConnectionCount));
}
<h4 class="modal-title">
    Connections <span class="text-muted small">(<a href="@Url.Action(nameof(SQLController.Connections), new { node = Model.Name })">View Detail</a>)</span>  
</h4>
@if (connections.Any())
{
    <table class="table table-striped table-hover text-nowrap table-super-condensed table-responsive">
        <thead>
            <tr>
                <th>Login</th>
                <th>Isolation</th>
                <th>Count</th>
                <th>Total Reads</th>
                <th>Total Writes</th>
                <th>Last Login</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var c in groups)
            {
                <tr>
                    <td>@c.Key.LoginName</td>
                    <td>@c.Key.TransactionIsolationLevel.AsString(EnumFormat.Description)</td>
                    <td>@c.Sum(s => s.ConnectionCount).ToComma()</td>
                    <td>@c.Sum(s => s.TotalReads).ToComma()</td>
                    <td>@c.Sum(s => s.TotalWrites).ToComma()</td>
                    <td>@c.Max(s => s.LastConnectTime).ToRelativeTimeSpan()</td>
                </tr>
            }
            @if (groups.Count() > 2)
            {
                <tr class="total-row">
                    <td><b>Total</b></td>
                    <td></td>
                    <td>@connections.Sum(c => c.ConnectionCount).ToComma()</td>
                    <td>@connections.Sum(c => c.TotalReads).ToComma()</td>
                    <td>@connections.Sum(c => c.TotalWrites).ToComma()</td>
                    <td></td>
                </tr>
            }
        </tbody>
    </table>
}
else
{
    <div class="no-content">No Acitve Connections</div>
}